﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XmlServer.ServiceContracts;
using XmlServer.ServiceImplementation;


namespace TimeOperator
{
    public class Time : IOperator
    {
        public const string SERVER_NAME = "Time Operator";
        public const string SERVER_VERSION = "1.0";

        public string Name
        {
            get { return SERVER_NAME; }
        }

        public string Version
        {
            get { return SERVER_VERSION; }
        }

        public string FullName
        {
            get { return String.Format("{0}_{1}", SERVER_NAME, Version); }
        }

        public string Description
        {
            get { return "This operator times for various time zones."; }
        }

        public List<IFact> Facts
        {
            get { return null; }
        }

        public IResponse ProcessRequest(IRequest request)
        {
            Response response = new Response();
            response.Request = request;     // return the original request object
            response.Success = true;        // assume a success

            try
            {

                List<IFact> facts = new List<IFact>();

                // get all the time zones on this computer
                var timeZones = TimeZoneInfo.GetSystemTimeZones();

                foreach (TimeZoneInfo timeZone in timeZones)
                {
                    var dateTime = TimeZoneInfo.ConvertTime(DateTime.Now, timeZone);
                    facts.Add(new Fact { Name = timeZone.StandardName, Value = String.Format("{0} {1}", dateTime.ToLongDateString(), dateTime.ToShortTimeString()) });                    
                }

                response.Facts = facts;

            }
            catch (Exception ex)
            {
                response.Facts = XmlServer.Helper.Utilities.CreateException(ex);
                response.Success = false;
            }

            response.StopProcessing = System.Environment.TickCount;
            return response;

        }
    }
}
